When sending data to Algolia, it’s best to send several records simultaneously instead of individually. It reduces network calls and speeds up indexing, especially when you have a lot of records, but everyone should send indexing operations in batches whenever possible.

For example, you might decide to send all the data from your database and end up with a million records to index. That’s too big to send all at once because Algolia limits you to 1 GB per batch per request. In reality, sending that much data in a single network call would fail before reaching the API. You could loop over each record and send them with the saveObjects method. The problem is that you would perform a million individual network calls, which would take way too long and saturate your Algolia cluster with indexing jobs.

A leaner approach is to split your collection of records into smaller collections, then send each chunk one by one. For optimal indexing performance, aim for a batch size of about 10 MB, representing between 1,000 and 10,000 records, depending on the average record size.

  • Batching records doesn’t reduce your operations count. Algolia counts indexing operations per record, not per method call, so from a pricing perspective, batching records is the same as indexing records individually.
  • Be careful when approaching your plan’s maximum number of records. If you’re close to the record limit, batch operations may fail. The error message “You have exceeded your Record quota” means the engine doesn’t know if the batch operation will update records or add new ones. If this happens, upgrade to a plan with a higher record limit or reduce your batch size.

Using the API

When using the saveObjects method, the API client automatically chunks your records into batches of 1,000 objects.

If you want to upload large files, consider using the Algolia CLI with the algolia objects import command.

using System;
using System.Collections.Generic;
using System.IO;
using Algolia.Search.Clients;
using Newtonsoft.Json;

public class Actor
{
    public string Name { get; set; }
    public string ObjectId { get; set; }
    public int Rating { get; set; }
    public string ImagePath { get; set; }
    public string AlternativePath { get; set; }
}

public class AlgoliaIntegration
{
    private SearchClient client;
    private SearchIndex index;

    public AlgoliaIntegration(string ApplicationID, string apiKey)
    {
        client = new SearchClient("YourApplicationID", "YourWriteAPIKey");
        index = client.InitIndex("actors");

        // Assuming the actors.json file is in the same directory as the executable
        string json = File.ReadAllText("actors.json");
        var settings = new JsonSerializerSettings
        {
            ContractResolver =
                new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
        };
        IEnumerable<Actor> actors = JsonConvert.DeserializeObject<IEnumerable<Actor>>(
            json,
            settings
        );

        // Batching/Chunking is done automatically by the API client
        bool autoGenerateObjectIDIfNotExist = true;
        index.SaveObjects(actors, autoGenerateObjectIDIfNotExist);
    }
}

// To use the above class, you would instantiate it somewhere in your application startup logic
// Example:
// var algoliaIntegration = new AlgoliaIntegration("YourApplicationID", "YourWriteAPIKey");

With this approach, you would make 100 API calls instead of 1,000,000. Depending on your records’ sizes and your network speed, you could create bigger or smaller chunks.

Using the dashboard

You can also send your records in your Algolia dashboard.

Add records manually

  1. Go to your dashboard, select the Data Sources icon, and then select your index.
  2. Click the Add records tab and select Add manually.
  3. Copy/paste your chunk in the JSON editor, then click Push record.
  4. Repeat for all your chunks.

Upload a file

  1. Go to your dashboard and select your index.
  2. Click the Add records tab and select Upload file.
  3. Either click the file upload area to select the file where your chunk is or drag it onto the page.
  4. Upload starts automatically.
  5. Repeat for all your chunks.